home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / Session.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  9.8 KB  |  263 lines

  1. package symantec.itools.db.pro;
  2.  
  3. import java.util.Hashtable;
  4. import java.util.NoSuchElementException;
  5. import java.util.Properties;
  6. import java.util.StringTokenizer;
  7. import java.util.Vector;
  8. import symantec.itools.db.net.ClientSession;
  9. import symantec.itools.db.net.Param;
  10. import symantec.itools.db.net.RemoteObject;
  11. import symantec.itools.db.net.SQLConnectionException;
  12. import symantec.itools.db.net.TextParam;
  13. import symjava.sql.SQLException;
  14.  
  15. public class Session {
  16.    private boolean _isClosed;
  17.    ClientSession _session;
  18.    RemoteObject _messgr;
  19.    Vector _metadataObjs;
  20.    Vector _multiViews;
  21.    Logon _logonObj;
  22.    ConnectionInfo _defaultConn;
  23.    private String _serverURL;
  24.    private final int METHOD_close;
  25.    private final int METHOD_getMetaData = 1;
  26.    private final int METHOD_createView = 2;
  27.    private final int METHOD_inquireDSN = 3;
  28.  
  29.    public Session(String serverURL) throws SQLException {
  30.       this.connect(serverURL);
  31.       this._serverURL = serverURL;
  32.       Vector params = new Vector();
  33.       int objectID = this._messgr.invokeConstructor(1, params);
  34.       this._messgr.setObject("CSCLClientSession", objectID);
  35.       this._isClosed = false;
  36.       this._defaultConn = new ConnectionInfo("");
  37.    }
  38.  
  39.    public String getServerURL() throws SQLException {
  40.       return this._serverURL;
  41.    }
  42.  
  43.    public void setLogonObject(Logon logonObj) throws SQLException {
  44.       this._logonObj = logonObj;
  45.    }
  46.  
  47.    public void setDefaultConnection(ConnectionInfo conn) throws SQLException {
  48.       this._defaultConn = conn;
  49.    }
  50.  
  51.    public RelationView createView(ConnectionInfo conn, String sql, Properties requestProps) throws SQLException {
  52.       Hashtable ht = RelationView.convertViewProps(requestProps);
  53.       MultiView mv = this.createMultiView(conn, sql, ht);
  54.       this.addMultiView(mv);
  55.       RelationView rv = new RelationView(mv, (byte)0, conn);
  56.       mv.setRootRelView(rv);
  57.       rv.setInitialPosition(requestProps);
  58.       return rv;
  59.    }
  60.  
  61.    MultiView createMultiView(ConnectionInfo conn, String sql, Hashtable ht) throws SQLException {
  62.       if (conn == null) {
  63.          throw new SQLException("ConnectionInfo object is null");
  64.       } else {
  65.          Integer optConc = (Integer)ht.get("optConc");
  66.          Integer maxRows = (Integer)ht.get("maxRecords");
  67.          Boolean readOnly = (Boolean)ht.get("readOnly");
  68.          Boolean sharable = (Boolean)ht.get("sharable");
  69.          String viewName = new String("RelView");
  70.          ht.get("recPosition");
  71.          int retries = 0;
  72.  
  73.          while(true) {
  74.             String username = conn.getUser();
  75.             String password = conn.getPassword();
  76.             String db = conn.getDBString();
  77.             Vector params = new Vector();
  78.             params.addElement(new TextParam(0, db));
  79.             params.addElement(new TextParam(0, viewName));
  80.             params.addElement(new TextParam(0, sql));
  81.             params.addElement(new Param(0, optConc));
  82.             params.addElement(new Param(0, maxRows));
  83.             params.addElement(new Param(0, readOnly));
  84.             params.addElement(new Param(0, sharable));
  85.             params.addElement(new TextParam(0, username));
  86.             params.addElement(new TextParam(0, password));
  87.             params.addElement(new Param(0, conn.getAutoDisconnect()));
  88.  
  89.             try {
  90.                Vector results = this._messgr.invokeMethod(2, params);
  91.                MultiView mv = new MultiView(this, results, conn);
  92.                return mv;
  93.             } catch (SQLConnectionException e) {
  94.                ++retries;
  95.                if (!this.logonFailed(conn, retries)) {
  96.                   throw e;
  97.                }
  98.             }
  99.          }
  100.       }
  101.    }
  102.  
  103.    public RelationView createView(ConnectionInfo conn, String tablename, Vector columns, Properties requestProps) throws SQLException {
  104.       String sql = new String("select ");
  105.       if (columns == null) {
  106.          sql = sql + "* ";
  107.       } else if (columns.size() == 0) {
  108.          sql = sql + "* ";
  109.       } else {
  110.          String temp = (String)columns.elementAt(0);
  111.          sql = sql + temp;
  112.  
  113.          for(int i = 1; i < columns.size(); ++i) {
  114.             temp = (String)columns.elementAt(i);
  115.             sql = sql + ", " + temp;
  116.          }
  117.       }
  118.  
  119.       sql = sql + " from " + tablename;
  120.       return this.createView(conn, sql, requestProps);
  121.    }
  122.  
  123.    public RelationView createView(ConnectionInfo conn, String sql) throws SQLException {
  124.       return this.createView(conn, sql, new Properties());
  125.    }
  126.  
  127.    public RelationView createView(ConnectionInfo conn, String tablename, Vector columns) throws SQLException {
  128.       return this.createView(conn, tablename, columns, new Properties());
  129.    }
  130.  
  131.    public RelationView createView(String sql) throws SQLException {
  132.       return this.createView(this._defaultConn, sql);
  133.    }
  134.  
  135.    public RelationView createView(String tablename, Vector columns) throws SQLException {
  136.       return this.createView(this._defaultConn, tablename, columns);
  137.    }
  138.  
  139.    public Request getRequest() throws SQLException {
  140.       return this.getRequest("");
  141.    }
  142.  
  143.    public Request getRequest(String sql) throws SQLException {
  144.       Request request = new Request(this, this._defaultConn);
  145.       request.setSQL(sql);
  146.       return request;
  147.    }
  148.  
  149.    public SessionMetaData getMetaData(ConnectionInfo conn) throws SQLException {
  150.       int retries = 0;
  151.  
  152.       while(true) {
  153.          String name = conn.getUser();
  154.          String password = conn.getPassword();
  155.          String db = conn.getDBString();
  156.          Vector params = new Vector();
  157.          params.addElement(new TextParam(0, db));
  158.          params.addElement(new TextParam(0, name));
  159.          params.addElement(new TextParam(0, password));
  160.  
  161.          try {
  162.             Vector results = this._messgr.invokeMethod(1, params);
  163.             SessionMetaData md = new SessionMetaData(this, results, conn);
  164.             this._metadataObjs.addElement(md);
  165.             return md;
  166.          } catch (SQLConnectionException e) {
  167.             ++retries;
  168.             if (!this.logonFailed(conn, retries)) {
  169.                throw e;
  170.             }
  171.          }
  172.       }
  173.    }
  174.  
  175.    public RelationView inquireDSN() throws SQLException {
  176.       Vector results = this._messgr.invokeMethod(3);
  177.       MultiView mv = new MultiView(this, results, new ConnectionInfo(""));
  178.       this.addMultiView(mv);
  179.       return mv.getRootRelView();
  180.    }
  181.  
  182.    public void close() throws SQLException {
  183.       if (!this._isClosed) {
  184.          for(int i = 0; i < this._multiViews.size(); ++i) {
  185.             MultiView mv = (MultiView)this._multiViews.elementAt(i);
  186.  
  187.             try {
  188.                mv.close();
  189.             } catch (SQLException var3) {
  190.             }
  191.          }
  192.  
  193.          this._multiViews.removeAllElements();
  194.          this._metadataObjs.removeAllElements();
  195.          this._messgr.invokeMethod(0);
  196.          this._session.close();
  197.          this._session = null;
  198.          this._messgr = null;
  199.          this._isClosed = true;
  200.       }
  201.  
  202.    }
  203.  
  204.    private StringTokenizer parseURL(String url) throws SQLException {
  205.       try {
  206.          StringTokenizer tokens = new StringTokenizer(url);
  207.          String protocol = tokens.nextToken(":");
  208.          if (!protocol.equals("dbaw")) {
  209.             throw new SQLException("Unrecognized protocol.");
  210.          } else {
  211.             String subname = tokens.nextToken("");
  212.             return new StringTokenizer(subname);
  213.          }
  214.       } catch (NoSuchElementException var5) {
  215.          throw new SQLException("Invalid URL syntax.");
  216.       }
  217.    }
  218.  
  219.    private Properties parseSubname(StringTokenizer tokens) throws SQLException {
  220.       try {
  221.          Properties properties = new Properties();
  222.          String host = tokens.nextToken(":/");
  223.          String port = tokens.nextToken(":/");
  224.          ((Hashtable)properties).put("host", host);
  225.          ((Hashtable)properties).put("port", port);
  226.          return properties;
  227.       } catch (NoSuchElementException var5) {
  228.          throw new SQLException("Invalid subname syntax.");
  229.       }
  230.    }
  231.  
  232.    private void connect(String url) throws SQLException {
  233.       this._session = null;
  234.       this._messgr = null;
  235.       this._isClosed = true;
  236.       this._metadataObjs = new Vector();
  237.       this._multiViews = new Vector();
  238.  
  239.       try {
  240.          StringTokenizer subname = this.parseURL(url);
  241.          Properties connectProperties = this.parseSubname(subname);
  242.          String host = connectProperties.getProperty("host", "");
  243.          int portNumber = Integer.parseInt(connectProperties.getProperty("port", "0"));
  244.          this._session = new ClientSession(host, portNumber, "", true);
  245.          this._messgr = new RemoteObject("CSCLClientSession", 0, this._session);
  246.       } catch (Exception e) {
  247.          throw new SQLException(((Throwable)e).toString());
  248.       }
  249.    }
  250.  
  251.    boolean logonFailed(ConnectionInfo conn, int retries) {
  252.       return this._logonObj == null ? false : this._logonObj.logonFailed(conn, retries);
  253.    }
  254.  
  255.    ClientSession getClientSession() {
  256.       return this._session;
  257.    }
  258.  
  259.    void addMultiView(MultiView mv) {
  260.       this._multiViews.addElement(mv);
  261.    }
  262. }
  263.